
/*
 * Fibo.java
 */
import java.text.DecimalFormat;
import java.util.Vector;

public class Fibo extends Thread {
	private Vector<ThreadFibo> worker;
	private TimeStamp ts;
	private int n, numThread;
	private Thread t;
	private boolean debug;
	
	public Fibo(int n, int numThread, boolean debug,  boolean thread) {
		this.debug = debug;
		this.n = n;
		this.numThread = numThread;
		worker = new Vector<ThreadFibo>();
		ts = new TimeStamp();
		if(thread) {
			t = new Thread(this);
			t.start();
		}
		else {
			findFibo();
		}
	}// end default constructor
	
	public void run() {
		findFibo();
	}// end method run
	
	public void findFibo() {
		ts.start();
		for(int i = 0; i < numThread; i++) {
			worker.add(new ThreadFibo(n));
		}
		
		try {
			for(int i = 0; i<numThread; i++) 
				worker.elementAt(i).getThread().join();
		}catch(InterruptedException e) {	
			System.out.println("Main thread Interrupted");
			e.printStackTrace();
		}
		
		ts.stop();
		if(debug)
			printResult();
	}// end method findFibo
	
	private void printResult() {
		DecimalFormat formatter = new DecimalFormat("#,###,###");
		System.out.println("Done finding fibonacci number.");
	    System.out.println("execution time for Fibo wokload is: "
	    		+ formatter.format(ts.elapsedTime()) + " nanoseconds.");
	}// end method printResult
	
	public double getExecutionTime() {
		return this.ts.elapsedTime();
	}// end method getExecutionTime
}// end class Fibo
